package org.jeecg.modules.friends.trade;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.friends.handler.AbstractTradeOrderHandler;
import org.jeecg.modules.friends.utils.MD5Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * @author youzaner
 * @date 2019-08-19
 */
@RestController
@RequestMapping("/friends/youzan/order")
@Slf4j
public class YouzanTradeOrderController {
    private static final String CLIENT_ID="7964bf355626a10039"; //应用的 client_id
    private static final String CLIENT_SECRET="7e5b984a76dae11f5064039b1c1eee93";//应用的 client_secret

    @PostMapping("/push")
    public Result<?> test(@RequestBody String entity,
            @RequestHeader("Event-Sign") String eventSign,
            @RequestHeader("Event-Type") String eventType,
            @RequestHeader("Client-Id") String clientId){
        /**
         * 判断消息是否合法
         * md5方法可参考 https://doc.youzanyun.com/doc#/content/27034/29010/34663
         */
        log.info("收到有赞消息:entity: {}", entity);

        String sign = MD5Utils.MD5(CLIENT_ID + entity + CLIENT_SECRET);
        if(!sign.equals(eventSign)){
            return Result.error("订单失败");
        }
        AbstractTradeOrderHandler.processDispose(eventType, entity);

        return Result.OK("成功");
    }
    private JSONObject buildSuccessResponse(){
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");
        return res;
    }
    private JSONObject buildFailedResponse(){
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "failed");
        return res;
    }
}